{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitec4538a0ed7a4029b9bd19594323cc7e",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import fetch_openml\n",
    "# 这个数据集需要下一会，不过下载好了有缓存\n",
    "#　跟课堂上的数据是一样的。\n",
    "mnist_data = fetch_openml(\"mnist_784\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "X = mnist_data.data\n",
    "y= np.array([int(i) for i in mnist_data.target])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "left = np.hstack((X[0].reshape(28,28)[:, 1:], np.zeros((28,1)))).reshape(-1)\n",
    "right = np.hstack((np.zeros((28,1)),X[0].reshape(28,28)[:, :-1])).reshape(-1)\n",
    "up = np.vstack((X[0].reshape(28,28)[1:], np.zeros((28)))).reshape(-1)\n",
    "down = np.vstack((np.zeros((28)),X[0].reshape(28,28)[:-1])).reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "def show_img(img):\n",
    "    plt.imshow(img.reshape(28,28), cmap=matplotlib.cm.binary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def move_left(img):\n",
    "    return np.hstack((img.reshape(28,28)[:, 1:], np.zeros((28,1)))).reshape(-1)\n",
    "def move_right(img):\n",
    "    return np.hstack((np.zeros((28,1)),img.reshape(28,28)[:, :-1])).reshape(-1)\n",
    "def move_up(img):\n",
    "    return np.vstack((img.reshape(28,28)[1:], np.zeros((28)))).reshape(-1)\n",
    "def move_down(img):\n",
    "    return np.vstack((np.zeros((28)),img.reshape(28,28)[:-1])).reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_expanded = np.vstack(\n",
    "    [move_left(i) for i in X] +\n",
    "    [move_right(i) for i in X] +\n",
    "    [move_up(i) for i in X] +\n",
    "    [move_down(i) for i in X] + \n",
    "    [i for i in X]\n",
    ")\n",
    "y_expanded = np.hstack([y for i in range(5)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X_expanded, y_expanded, stratify = y_expanded)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n                     metric_params=None, n_jobs=None, n_neighbors=5, p=2,\n                     weights='uniform')"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn_clf = KNeighborsClassifier()\n",
    "knn_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.9810742857142857"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "accuracy_score(y_test, y_pred)"
   ]
  }
 ]
}